/*
 * @Description: 
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-06-09 11:20:34
 * @LastEditors: TT
 * @LastEditTime: 2022-06-22 16:16:18
 */
import 'package:flutter/material.dart';
import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import '../config/config_index.dart';
import 'common_place_holder_widget.dart';

abstract class CommonStateListWidget<T extends TTNormalStatefulWidget>
    extends TTNormalState<T>
    with AbstracRefreshWidget, AbstractNetWork, AbstracRefreshMehod {
  /// 属性
  ///
  ///
  CommonPlaceHoldType placeHoldType = CommonPlaceHoldType.nologin;
  EasyRefreshController? refreshController;

  @override
  void initDefaultState() {
    refreshController = EasyRefreshController();
    super.initDefaultState();
  }

  @override
  Widget createBody(BuildContext context) {
    return createRefreshWidget(context);
  }

  @override
  Widget createRefreshWidget(BuildContext context) {
    return EasyRefresh(
      controller: refreshController,
      onRefresh: () async {},
      onLoad: () async {},
      child: createListView(context),
      header: createHeader(),
      footer: createFooter(),
      emptyWidget: createEmptyWidget(),
    );
  }

  /// 自定义刷新控件 header
  Header? createHeader() {
    return ClassicalHeader(
      refreshingText: '正在刷新...',
      refreshedText: '刷新成功',
      refreshReadyText: '松开刷新',
      refreshFailedText: '刷新失败',
      refreshText: '下拉刷新',
      showInfo: false,
      textColor: HzyColorConfig().col000000,
    );
  }

  /// 自定义刷新控件 footer
  Footer? createFooter() {
    return ClassicalFooter(
      loadText: "上啦加载更多数据",
      loadReadyText: "松开加载",
      loadingText: "正在加载...",
      loadedText: "加载成功",
      loadFailedText: "加载失败",
      noMoreText: "没有更多数据了~",
      showInfo: false,
      textColor: HzyColorConfig().col000000,
    );
  }

  /// 创建失败 界面
  @override
  Widget? createErrorWidget() {
    return CommonPlaceHoldPage(
      placeHoldType: placeHoldType,
    );
  }

  @override
  Color? createLeadingIconColor() {
    return HzyColorConfig().col000000;
  }

  @override
  Color? createAppBarTextColor() {
    return HzyColorConfig().col000000;
  }

  // 下啦刷新触发事件
  @override
  void configRefresh() {
    page = 1;
    getnetworkdata(1, configNetWorkParmas());
  }

  // 上啦加载更多触发事件
  @override
  void configLoading() {
    page++;
    getnetworkdata(2, configNetWorkParmas());
  }

  /// 配置网络请求参数
  @override
  Map<String, dynamic>? configNetWorkParmas() {
    return {};
  }

  /// 配置网络请求
  @override
  void getnetworkdata(int? type, Map<String, dynamic>? info) async {}

  /// 结束刷新
  @override
  void endRefresh(int type, PageState pageState) {
    bool success = true;
    if (pageState == PageState.errorState ||
        pageState == PageState.erroronlyTotal ||
        pageState == PageState.errorshowRelesh) {
      success = false;
    }
    if (refreshController != null) {
      if (type == 1) {
        refreshController!.finishRefresh(success: success);
      } else if (type == 2) {
        bool noMore = false;
        if (pageState == PageState.noMoreDataState) {
          noMore = true;
        }
        refreshController!.finishLoad(success: success, noMore: noMore);
      }
    }
    configsetState(() {});
  }
}
